package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.service.UserAPI;
import com.stylefeng.guns.api.film.vo.UserInfoModel;
import com.stylefeng.guns.api.film.vo.UserModel;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MoocUserTMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @创建人 lIUJF
 * @创建时间 2019/1/4
 * @描述: 微服务中，service...即代表对外提供服务，应注意规范。和传统项目区分开。
 */
@Component
@Service(interfaceClass = UserAPI.class,loadbalance ="roundrobin") //暴露服务
public class UserServiceImpl implements UserAPI {

	@Autowired
	private MoocUserTMapper moocUserTMapper;

	@Override
	public int login(String username, String password){
		//登录账户获取数据库信息
		MoocUserT userT = new MoocUserT();
		userT.setUserName(username);
		userT = moocUserTMapper.selectOne(userT);
		if (null != userT && userT.getUuid() > 0) {
			//获取到的结果，与加密密码匹配
			if (MD5Util.encrypt(password).equals(userT.getUserPwd())) {
				return userT.getUuid();
			} else {
				return 0;
			}

		} else {
			return 0;
		}
	}

	@Override
	public boolean register(UserModel userModel){
		//将注册信息实体转换为数据实体
		MoocUserT userT = new MoocUserT();
		userT.setUserName(userModel.getUserName());
		userT.setEmail(userModel.getEmail());
		userT.setAddress(userModel.getAddress());
		userT.setUserPhone(userModel.getPhone());
		//密码加密处理 [md5混淆加密]
		String md5PassWord = MD5Util.encrypt(userModel.getPassWord());
		userT.setUserPwd(md5PassWord);
		//将数据实体存入数据库
		Integer result = moocUserTMapper.insert(userT);
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean checkUserName(String userName){
		//登录账户获取数据库信息
		EntityWrapper<MoocUserT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("user_name", userName);
		Integer count = moocUserTMapper.selectCount(entityWrapper);
		if (null != count && count > 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public UserInfoModel getUserInfo(int uuid){
		MoocUserT userT = moocUserTMapper.selectById(uuid);
		// 将MoocUserT转换UserInfoModel
		UserInfoModel userInfoModel = do2UserInfo(userT);
		// 返回UserInfoModel
		return userInfoModel;
	}

	@Override
	public UserInfoModel updateUserInfo(UserInfoModel userInfoModel){
		// 将传入的参数转换为DO 【MoocUserT】
		MoocUserT moocUserT = new MoocUserT();
		moocUserT.setUuid(userInfoModel.getUuid());
		moocUserT.setNickName(userInfoModel.getNickname());
		moocUserT.setLifeState(Integer.parseInt(userInfoModel.getLifeState()));
		moocUserT.setBirthday(userInfoModel.getBirthday());
		moocUserT.setBiography(userInfoModel.getBiography());
		//moocUserT.setBeginTime(time2Date(userInfoModel.getBeginTime()));
		//moocUserT.setUpdateTime(time2Date(userInfoModel.getUpdateTime()));
		moocUserT.setHeadUrl(userInfoModel.getHeadAddress());
		moocUserT.setEmail(userInfoModel.getEmail());
		moocUserT.setAddress(userInfoModel.getAddress());
		moocUserT.setUserPhone(userInfoModel.getPhone());
		moocUserT.setUserSex(userInfoModel.getSex());
		// DO存入数据库
		Integer integer = moocUserTMapper.updateById(moocUserT);
		if (integer > 0) {
			// 将数据从数据库中读取出来
			UserInfoModel userInfo = getUserInfo(moocUserT.getUuid());
			// 将结果返回给前端
			return userInfo;
		} else {
			return null;
		}
	}

	private UserInfoModel do2UserInfo(MoocUserT moocUserT){
		UserInfoModel userInfoModel = new UserInfoModel();
		userInfoModel.setUuid(moocUserT.getUuid());
		userInfoModel.setHeadAddress(moocUserT.getHeadUrl());
		userInfoModel.setPhone(moocUserT.getUserPhone());
		userInfoModel.setUpdateTime(moocUserT.getUpdateTime().getTime());
		userInfoModel.setEmail(moocUserT.getEmail());
		userInfoModel.setUsername(moocUserT.getUserName());
		userInfoModel.setNickname(moocUserT.getNickName());
		userInfoModel.setLifeState("" + moocUserT.getLifeState());
		userInfoModel.setBirthday(moocUserT.getBirthday());
		userInfoModel.setAddress(moocUserT.getAddress());
		if(moocUserT.getUserSex()!=null){
			userInfoModel.setSex(moocUserT.getUserSex());
		}
		userInfoModel.setBeginTime(moocUserT.getBeginTime().getTime());
		userInfoModel.setBiography(moocUserT.getBiography());
		return userInfoModel;
	}

	private Date time2Date(long time){
		Date date = new Date(time);
		return date;
	}
}
